package com.thtf.strokeinquiry.app.service.imp;

import com.thtf.strokeinquiry.app.bean.*;
import com.thtf.strokeinquiry.app.bean.req.*;
import com.thtf.strokeinquiry.app.bean.resp.MedInquiryList;
import com.thtf.strokeinquiry.app.dao.*;
import com.thtf.strokeinquiry.app.service.MedInquiryRecordService;
import com.thtf.strokeinquiry.app.util.CodeUtil;
import com.thtf.strokeinquiry.app.util.DateConvertUtil;
import com.thtf.strokeinquiry.app.util.DefaultValueUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MedInquiryRecordServiceImp implements MedInquiryRecordService {

    @Autowired
    private MedInquiryRecordMapper medInquiryRecordMapper ;

    @Autowired
    private MedPatientInfoMapper medPatientInfoMapper;

    @Autowired
    private MedInquiryLifestyleMapper medInquiryLifestyleMapper;

    @Autowired
    private MedInquiryFamilyhistoryMapper medInquiryFamilyhistoryMapper ;

    @Autowired
    private MedInquiryPrevioushistoryMapper medInquiryPrevioushistoryMapper ;

    @Autowired
    private MedInquiryPhysicalexamMapper medInquiryPhysicalexamMapper;

    @Autowired
    private MedInquiryLabinspectionMapper medInquiryLabinspectionMapper ;

    @Autowired
    private  MedInquiryEcgMapper  ecgMapper ;

    @Autowired
    private MedInquiryRiskassMapper riskassMapper;

    @Autowired
    private  MedInquiryBloodvesselsMapper bloodvesselsMapper ;

    @Autowired
    private  MedInquirySurgeryMapper surgeryMapper ;

    @Autowired
    private  MedInquiryLogMapper logMapper ;


    @Override
    public MedInquiryRecord saveMedInquiryRecord(MedPatientInfoReq req) {
        if(!StringUtils.isEmpty(req.getPatientIdNo())){

            MedInquiryRecord data =getMedInquiryRecordByIdNO(req.getPatientIdNo());
            if(data!=null){
                MedInquiryRecord updateRecord=new MedInquiryRecord();
                updateRecord.setId(data.getId());
                BeanUtils.copyProperties(req,updateRecord);
                updateRecord.setUpdateDate(new Date());
                medInquiryRecordMapper.updateByPrimaryKeySelective(updateRecord);
                if(data.getPatientId()!=null){
                    MedPatientInfo patientInfoQuery=new MedPatientInfo();
                    patientInfoQuery.setPatientId(data.getPatientId());
                    MedPatientInfo patientInfoData= medPatientInfoMapper.selectOne(patientInfoQuery);
                    if(patientInfoData!=null){
                        MedPatientInfo patientUpdate=new MedPatientInfo();
                        patientUpdate.setId(patientInfoData.getId());
                        BeanUtils.copyProperties(req,patientUpdate);
                        patientUpdate.setPatientId(null);
                        patientUpdate.setUpdateDate(new Date());
                        medPatientInfoMapper.updateByPrimaryKeySelective(patientUpdate);
                    }
                }
                logMapper.insertSelective(new MedInquiryLog(data.getRecordId(),"baicInfo",1,"update",new Date()));
                updateRecord.setRecordId(data.getRecordId());
                return  updateRecord;
            }
        }

        String recoderId = CodeUtil.generationCodeRecordId();
        String patientId= CodeUtil.generationCodePatientId();
        MedInquiryRecord record=new MedInquiryRecord();
        MedPatientInfo patientInfo=new MedPatientInfo();
        BeanUtils.copyProperties(req,record);
        BeanUtils.copyProperties(req,patientInfo);
        record.setRecordId(recoderId);
        record.setPatientId(patientId);
        record.setCreateDate(new Date());
        record.setUpdateDate(new Date());
        patientInfo.setPatientId(patientId);
        patientInfo.setCreateDate(new Date());
        patientInfo.setUpdateDate(new Date());
        medInquiryRecordMapper.insertSelective(record);
        medPatientInfoMapper.insertSelective(patientInfo);
        logMapper.insertSelective(new MedInquiryLog(recoderId,"baicInfo",1,"insert",new Date()));


        return record;
    }

    @Override
    public MedLifestyleReq saveMedLifeStyle(MedLifestyleReq req) {
        String recordId=req.getRecordId();
        MedInquiryRecord record=new MedInquiryRecord();
        record.setRecordId(recordId);
        MedInquiryRecord data =medInquiryRecordMapper.selectOne(record);
        if(data==null){
            return  null;
        }
        MedInquiryLifestyle  query =new MedInquiryLifestyle();
        query.setRecordId(req.getRecordId());
        MedInquiryLifestyle  lifestyleData= medInquiryLifestyleMapper.selectOne(query);
        if(lifestyleData!=null){
            MedInquiryLifestyle  medInquiryLifestyle=new MedInquiryLifestyle();
            medInquiryLifestyle.setId(lifestyleData.getId());
            BeanUtils.copyProperties(req,medInquiryLifestyle);
            medInquiryLifestyle.setRecordId(null);
            medInquiryLifestyle.setUpdateDate(new Date());
            medInquiryLifestyleMapper.updateByPrimaryKeySelective(medInquiryLifestyle);
            logMapper.insertSelective(new MedInquiryLog(recordId,"lifeStyle",2,"update",new Date()));
            return  req;
        }

        MedInquiryLifestyle  medInquiryLifestyle=new MedInquiryLifestyle();
        BeanUtils.copyProperties(req,medInquiryLifestyle);
        BeanUtils.copyProperties(data,medInquiryLifestyle);
        medInquiryLifestyle.setCreateDate(new Date());
        medInquiryLifestyle.setUpdateDate(new Date());
        medInquiryLifestyleMapper.insertSelective(medInquiryLifestyle);

        logMapper.insertSelective(new MedInquiryLog(recordId,"lifeStyle",2,"insert",new Date()));

        return req;
    }

    @Override
    public MedFamilyhistoryReq saveFamilyhistory(MedFamilyhistoryReq req) {
        MedInquiryRecord record=new MedInquiryRecord();
        record.setRecordId(req.getRecordId());
        MedInquiryRecord data =medInquiryRecordMapper.selectOne(record);
        if(data==null){
            return  null;
        }

        MedInquiryFamilyhistory  query =new MedInquiryFamilyhistory();
        query.setRecordId(req.getRecordId());
        MedInquiryFamilyhistory  queryData= medInquiryFamilyhistoryMapper.selectOne(query);
        if(queryData!=null){
            MedInquiryFamilyhistory  updateRecord=new MedInquiryFamilyhistory();
            updateRecord.setId(queryData.getId());
            BeanUtils.copyProperties(req,updateRecord);
            updateRecord.setRecordId(null);
            updateRecord.setUpdateDate(new Date());
            medInquiryFamilyhistoryMapper.updateByPrimaryKeySelective(updateRecord);

            logMapper.insertSelective(new MedInquiryLog(req.getRecordId(),"familyhistory",3,"update",new Date()));
            return  req;
        }

        MedInquiryFamilyhistory  medInquiryFamilyhistory=new MedInquiryFamilyhistory();
        BeanUtils.copyProperties(req,medInquiryFamilyhistory);
        BeanUtils.copyProperties(data,medInquiryFamilyhistory);
        medInquiryFamilyhistory.setCreateDate(new Date());
        medInquiryFamilyhistory.setUpdateDate(new Date());
        medInquiryFamilyhistoryMapper.insertSelective(medInquiryFamilyhistory);

        logMapper.insertSelective(new MedInquiryLog(req.getRecordId(),"familyhistory",3,"insert",new Date()));

        return req;

    }

    @Override
    public MedPrevioushistoryReq savePrevioushistory(MedPrevioushistoryReq req) {
        MedInquiryRecord record=new MedInquiryRecord();
        record.setRecordId(req.getRecordId());
        MedInquiryRecord data =medInquiryRecordMapper.selectOne(record);
        if(data==null){
            return  null;
        }
        MedInquiryPrevioushistory  query =new MedInquiryPrevioushistory();
        query.setRecordId(req.getRecordId());
        MedInquiryPrevioushistory  queryData= medInquiryPrevioushistoryMapper.selectOne(query);
        if(queryData!=null){
            MedInquiryPrevioushistory  updateRecord=new MedInquiryPrevioushistory();
            updateRecord.setId(queryData.getId());
            BeanUtils.copyProperties(req,updateRecord);
            updateRecord.setRecordId(null);
            updateRecord.setUpdateDate(new Date());
            medInquiryPrevioushistoryMapper.updateByPrimaryKeySelective(updateRecord);

            logMapper.insertSelective(new MedInquiryLog(req.getRecordId(),"previoushistory",4,"update",new Date()));

            return  req;
        }
        MedInquiryPrevioushistory medInquiryPrevioushistory=new MedInquiryPrevioushistory();
        BeanUtils.copyProperties(req,medInquiryPrevioushistory);
        BeanUtils.copyProperties(data,medInquiryPrevioushistory);
        medInquiryPrevioushistory.setCreateDate(new Date());
        medInquiryPrevioushistory.setUpdateDate(new Date());
        medInquiryPrevioushistoryMapper.insertSelective(medInquiryPrevioushistory);

        logMapper.insertSelective(new MedInquiryLog(req.getRecordId(),"previoushistory",4,"insert",new Date()));

        return req;
    }

    @Override
    public MedPhysicalexamReq savePhysicalExam(MedPhysicalexamReq req) {

        MedInquiryRecord record=new MedInquiryRecord();
        record.setRecordId(req.getRecordId());
        MedInquiryRecord data =medInquiryRecordMapper.selectOne(record);
        if(data==null){
            return  null;
        }
        MedInquiryPhysicalexam  query =new MedInquiryPhysicalexam();
        query.setRecordId(req.getRecordId());
        MedInquiryPhysicalexam  queryData= medInquiryPhysicalexamMapper.selectOne(query);
        if(queryData!=null){
            MedInquiryPhysicalexam  updateRecord=new MedInquiryPhysicalexam();
            updateRecord.setId(queryData.getId());
            BeanUtils.copyProperties(req,updateRecord);
            updateRecord.setRecordId(null);
            updateRecord.setUpdateDate(new Date());
            medInquiryPhysicalexamMapper.updateByPrimaryKeySelective(updateRecord);
            logMapper.insertSelective(new MedInquiryLog(req.getRecordId(),"physicalExam",5,"update",new Date()));

            return  req;
        }


        MedInquiryPhysicalexam physicalexam =new MedInquiryPhysicalexam();
        BeanUtils.copyProperties(req,physicalexam);
        BeanUtils.copyProperties(data,physicalexam);
        physicalexam.setCreateDate(new Date());
        physicalexam.setUpdateDate(new Date());
        medInquiryPhysicalexamMapper.insertSelective(physicalexam);

        logMapper.insertSelective(new MedInquiryLog(req.getRecordId(),"physicalExam",5,"insert",new Date()));

        return req;
    }

    @Override
    public MedLabinspectionReq saveLabinspection(MedLabinspectionReq req) {

        MedInquiryRecord record=new MedInquiryRecord();
        record.setRecordId(req.getRecordId());
        MedInquiryRecord data =medInquiryRecordMapper.selectOne(record);
        if(data==null){
            return  null;
        }
        MedInquiryLabinspection  query =new MedInquiryLabinspection();
        query.setRecordId(req.getRecordId());
        MedInquiryLabinspection  queryData= medInquiryLabinspectionMapper.selectOne(query);
        if(queryData!=null){
            MedInquiryLabinspection  updateRecord=new MedInquiryLabinspection();
            updateRecord.setId(queryData.getId());
            BeanUtils.copyProperties(req,updateRecord);
            updateRecord.setRecordId(null);
            updateRecord.setUpdateDate(new Date());
            medInquiryLabinspectionMapper.updateByPrimaryKeySelective(updateRecord);

            logMapper.insertSelective(new MedInquiryLog(req.getRecordId(),"labinspection",7,"update",new Date()));


            return  req;
        }
        MedInquiryLabinspection medInquiryLabinspection =new MedInquiryLabinspection();
        BeanUtils.copyProperties(req,medInquiryLabinspection);
        BeanUtils.copyProperties(data,medInquiryLabinspection);
        medInquiryLabinspection.setCreateDate(new Date());
        medInquiryLabinspection.setUpdateDate(new Date());
        medInquiryLabinspectionMapper.insertSelective(medInquiryLabinspection);

        logMapper.insertSelective(new MedInquiryLog(req.getRecordId(),"labinspection",7,"insert",new Date()));

        return req;
    }

    @Override
    public MedEcgReq saveECG(MedEcgReq req) {
        MedInquiryRecord record=new MedInquiryRecord();
        record.setRecordId(req.getRecordId());
        MedInquiryRecord data =medInquiryRecordMapper.selectOne(record);
        if(data==null){
            return  null;
        }

        MedInquiryEcg  query =new MedInquiryEcg();
        query.setRecordId(req.getRecordId());
        MedInquiryEcg  queryData= ecgMapper.selectOne(query);
        if(queryData!=null){
            MedInquiryEcg  updateRecord=new MedInquiryEcg();
            updateRecord.setId(queryData.getId());
            BeanUtils.copyProperties(req,updateRecord);
            updateRecord.setRecordId(null);
            updateRecord.setUpdateDate(new Date());
            ecgMapper.updateByPrimaryKeySelective(updateRecord);
            logMapper.insertSelective(new MedInquiryLog(req.getRecordId(),"ECG",6,"update",new Date()));

            return  req;
        }

        MedInquiryEcg ecg =new MedInquiryEcg();
        BeanUtils.copyProperties(req,ecg);
        BeanUtils.copyProperties(data,ecg);
        ecg.setCreateDate(new Date());
        ecg.setUpdateDate(new Date());
        ecgMapper.insertSelective(ecg);

        logMapper.insertSelective(new MedInquiryLog(req.getRecordId(),"ECG",6,"insert",new Date()));


        return req;
    }

    @Override
    public MedRiskassReq saveMedRiskass(MedRiskassReq req) {
        MedInquiryRecord record=new MedInquiryRecord();
        record.setRecordId(req.getRecordId());
        MedInquiryRecord data =medInquiryRecordMapper.selectOne(record);
        if(data==null){
           return null;
        }

        MedInquiryRiskass  query =new MedInquiryRiskass();
        query.setRecordId(req.getRecordId());
        MedInquiryRiskass  queryData= riskassMapper.selectOne(query);
        if(queryData!=null){
            MedInquiryRiskass  updateRecord=new MedInquiryRiskass();
            updateRecord.setId(queryData.getId());
            BeanUtils.copyProperties(req,updateRecord);
            updateRecord.setRecordId(null);
            updateRecord.setUpdateDate(new Date());
            riskassMapper.updateByPrimaryKeySelective(updateRecord);
            logMapper.insertSelective(new MedInquiryLog(req.getRecordId(),"riskass",8,"update",new Date()));
            return  req;
        }



        MedInquiryRiskass riskass =new MedInquiryRiskass();
        BeanUtils.copyProperties(req,riskass);
        BeanUtils.copyProperties(data,riskass);
        riskass.setCreateDate(new Date());
        riskass.setUpdateDate(new Date());
        riskassMapper.insertSelective(riskass);

        logMapper.insertSelective(new MedInquiryLog(req.getRecordId(),"riskass",8,"insert",new Date()));
        return req;
    }

    @Override
    public MedBloodvesselsReq saveBloodVessels(MedBloodvesselsReq req) {
        MedInquiryRecord record=new MedInquiryRecord();
        record.setRecordId(req.getRecordId());
        MedInquiryRecord data =medInquiryRecordMapper.selectOne(record);
        if(data==null){
            return  null;
        }

        MedInquiryBloodvessels  query =new MedInquiryBloodvessels();
        query.setRecordId(req.getRecordId());
        MedInquiryBloodvessels  queryData= bloodvesselsMapper.selectOne(query);
        if(queryData!=null){
            MedInquiryBloodvessels  updateRecord=new MedInquiryBloodvessels();
            updateRecord.setId(queryData.getId());
            BeanUtils.copyProperties(req,updateRecord);
            updateRecord.setRecordId(null);
            updateRecord.setUpdateDate(new Date());
            bloodvesselsMapper.updateByPrimaryKeySelective(updateRecord);

            logMapper.insertSelective(new MedInquiryLog(req.getRecordId(),"bloodVessels",9,"update",new Date()));

            return  req;
        }

        MedInquiryBloodvessels bloodvessels =new MedInquiryBloodvessels();
        BeanUtils.copyProperties(req,bloodvessels);
        BeanUtils.copyProperties(data,bloodvessels);
        bloodvessels.setCreateDate(new Date());
        bloodvessels.setUpdateDate(new Date());
        bloodvesselsMapper.insertSelective(bloodvessels);
        logMapper.insertSelective(new MedInquiryLog(req.getRecordId(),"bloodVessels",9,"insert",new Date()));


        return req;
    }

    @Override
    public MedSurgeryReq saveSurgery(MedSurgeryReq req) {
        MedInquiryRecord record=new MedInquiryRecord();
        record.setRecordId(req.getRecordId());
        MedInquiryRecord data =medInquiryRecordMapper.selectOne(record);
        if(data==null){
            return  null;
        }

        MedInquirySurgery  query =new MedInquirySurgery();
        query.setRecordId(req.getRecordId());
        MedInquirySurgery  queryData= surgeryMapper.selectOne(query);
        if(queryData!=null){
            MedInquirySurgery  updateRecord=new MedInquirySurgery();
            updateRecord.setId(queryData.getId());
            BeanUtils.copyProperties(req,updateRecord);
            updateRecord.setRecordId(null);
            updateRecord.setUpdateDate(new Date());
            surgeryMapper.updateByPrimaryKeySelective(updateRecord);
            logMapper.insertSelective(new MedInquiryLog(req.getRecordId(),"surgery",10,"update",new Date()));

            return  req;
        }

        MedInquirySurgery surgery =new MedInquirySurgery();
        BeanUtils.copyProperties(req,surgery);
        BeanUtils.copyProperties(data,surgery);
        surgery.setCreateDate(new Date());
        surgery.setUpdateDate(new Date());
        surgeryMapper.insertSelective(surgery);
        logMapper.insertSelective(new MedInquiryLog(req.getRecordId(),"surgery",10,"insert",new Date()));
        return req;
    }

    @Override
    public Object getDetial(String recordId, String type) {
        if("1".equals(type)){
            MedInquiryRecord query =new MedInquiryRecord();
            query.setRecordId(recordId);
            return  medInquiryRecordMapper.selectOne(query);
        }else if("2".equals(type)){
            MedInquiryLifestyle query =new MedInquiryLifestyle();
            query.setRecordId(recordId);
            return  medInquiryLifestyleMapper.selectOne(query);

        }else if("3".equals(type)){
            MedInquiryFamilyhistory query =new MedInquiryFamilyhistory();
            query.setRecordId(recordId);
            return  medInquiryFamilyhistoryMapper.selectOne(query);

        }else if("4".equals(type)){
            MedInquiryPrevioushistory query =new MedInquiryPrevioushistory();
            query.setRecordId(recordId);
            return  medInquiryPrevioushistoryMapper.selectOne(query);

        }else if("5".equals(type)){
            MedInquiryPhysicalexam query =new MedInquiryPhysicalexam();
            query.setRecordId(recordId);
            return  medInquiryPhysicalexamMapper.selectOne(query);

        }else if("6".equals(type)){
            MedInquiryEcg query =new MedInquiryEcg();
            query.setRecordId(recordId);
            return  ecgMapper.selectOne(query);

        }else if("7".equals(type)){
            MedInquiryLabinspection query =new MedInquiryLabinspection();
            query.setRecordId(recordId);
            return  medInquiryLabinspectionMapper.selectOne(query);

        }else if("8".equals(type)){
            MedInquiryRiskass query =new MedInquiryRiskass();
            query.setRecordId(recordId);
            return  riskassMapper.selectOne(query);

        }else if("9".equals(type)){
            MedInquiryBloodvessels query =new MedInquiryBloodvessels();
            query.setRecordId(recordId);
            return  bloodvesselsMapper.selectOne(query);

        }else if("10".equals(type)){
            MedInquirySurgery query =new MedInquirySurgery();
            query.setRecordId(recordId);
            return  surgeryMapper.selectOne(query);
        }
        return  null;
    }

    @Override
    public MedInquiryList getList(String idNo) {
        MedInquiryRecord data = getMedInquiryRecordByIdNO(idNo);
        if(data==null){
            return  DefaultValueUtil.getDefaultList();
        }
        MedInquiryList result =new MedInquiryList();
        Example example = new Example(MedInquiryLog.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("recordId",data.getRecordId());
        example.setOrderByClause("record_step_order desc");
        List<MedInquiryLog> dataList=logMapper.selectByExample(example);
        if(dataList==null||dataList.isEmpty()||dataList.size()==0){
            return  DefaultValueUtil.getDefaultList();
        }
        MedInquiryLog log =dataList.get(0);
        result.setRecordId(log.getRecordId());
        int step = log.getRecordStepOrder();
        List<MedInquiryList.Entity> list=new ArrayList<MedInquiryList.Entity>();
        for(int i=1;i<=10;i++){
            if(i<=(step+1)){
                list.add(new MedInquiryList.Entity(i+"","Y"));
            }
            else{
                list.add(new MedInquiryList.Entity(i+"","N"));
            }
        }
        result.setList(list);
        return result;
    }

    @Override
    public MedOrgReq saveInquiryOrg(MedOrgReq req) {

        MedInquiryRecord data =getMedInquiryRecordByIdNO(req.getPatientIdNo());
        if(data==null){
            return  null;
        }
        req.setRecordId(null);
        MedInquiryRecord update =new MedInquiryRecord();
        update.setId(data.getId());
        BeanUtils.copyProperties(req,update);
        medInquiryRecordMapper.updateByPrimaryKeySelective(update);
        req.setRecordId(data.getRecordId());
        return req;
    }

    private   MedInquiryRecord getMedInquiryRecordByIdNO(String idNO){
        if(StringUtils.isEmpty(idNO)){
            return null;
        }
       Example example = new Example(MedInquiryRecord.class);
       Example.Criteria criteria = example.createCriteria();
       criteria.andEqualTo("patientIdNo",idNO);
       String year= DateConvertUtil.covvert2String(new Date(),"yyyy");

       criteria.andGreaterThanOrEqualTo("createDate",year+"-01-01 00:00:00");
       criteria.andLessThanOrEqualTo("createDate",year+"-12-31 23:59:59");

       List<MedInquiryRecord> dataList=medInquiryRecordMapper.selectByExample(example);
       if(dataList==null||dataList.isEmpty()||dataList.size()==0){
           return  null;
       }
        return  dataList.get(0);
    }
}
